package com.comdo.newup_loan.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.comdo.newup_loan.R;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.MemberBean;
import com.comdo.newup_loan.util.StringUtils;

import java.util.List;

import kr.co.namee.permissiongen.PermissionGen;

/**
 * Created by liyuan on 2017/9/27.
 */

public class MemberListAdapter extends RecyclerView.Adapter<MemberListAdapter.ViewHolder> implements View.OnClickListener {
    private List<MemberBean.MemberItemBean> datas;
    private OnItemClickListener mOnItemClickListener = null;
    private Context context;

    public MemberListAdapter(List<MemberBean.MemberItemBean> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.itemView.setTag(position);
        viewHolder.mNameText.setText(datas.get(position).getStaffName());
        viewHolder.mJobText.setText(datas.get(position).getRoleName());
        viewHolder.mPhoneText.setText(datas.get(position).getPhone());
        viewHolder.mPhoneText.setId(position);
        viewHolder.mPhoneText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                requestPermiss();
                if (!StringUtils.isNull(datas.get(v.getId()).getPhone())) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    //builder.setIcon(R.drawable.ic_launcher);
                    builder.setTitle("确定拨打：");
                    builder.setMessage(datas.get(v.getId()).getPhone()+"?");
                    //    设置一个PositiveButton
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                if (context.checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {

                                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                                            + datas.get(v.getId()).getPhone()));
                                    context.startActivity(intent);
                                }
                            } else {
                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                                        + datas.get(v.getId()).getPhone()));
                                context.startActivity(intent);
                            }
                        }
                    });
                    builder.setNegativeButton("取消"
                            , new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    dialog.dismiss();
                                }
                            });
                    builder.show();


                }
            }
        });
        viewHolder.mTipText.setText(datas.get(position).getSaytoCustomer());
        Glide.with(context).load(Config.PIC_FORE + datas.get(position).getHeadPicUrl()).error(R.drawable.store_user_icon).into(viewHolder.mImageView);
    }

    private void requestPermiss() {
        PermissionGen.with((Activity) context)
                .addRequestCode(100)
                .permissions(
                        Manifest.permission.CALL_PHONE)
                .request();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_store_member, parent, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mNameText;
        public TextView mJobText;
        public TextView mPhoneText;
        public TextView mTipText;
        public ImageView mImageView;

        public ViewHolder(View view) {
            super(view);
            mNameText = (TextView) view.findViewById(R.id.tv_name);
            mJobText = (TextView) view.findViewById(R.id.tv_job);
            mPhoneText = (TextView) view.findViewById(R.id.tv_phone);
            mTipText = (TextView) view.findViewById(R.id.tv_tips);
            mImageView = (ImageView) view.findViewById(R.id.img_user);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
